package com.basic.business.declineBacklog.service.impl;

import com.basic.business.declineBacklog.domain.TDeclineBacklog;
import com.basic.business.declineBacklog.mapper.TDeclineBacklogMapper;
import com.basic.business.declineBacklog.service.ITDeclineBacklogService;
import com.basic.business.declineWarnBoard.vo.DeclineReplayCollectRespVo;
import com.basic.business.declineWarnBoard.vo.DeclineReplayPageReqVo;
import com.basic.business.declineWarnBoard.vo.DeclineReplayRespVo;
import com.basic.business.dingTalk.utils.DingTalkUtils;
import com.basic.business.labelMapping.domain.TLabelMapping;
import com.basic.business.labelMapping.mapper.TLabelMappingMapper;
import com.basic.business.linkBacklog.domain.TLinkBacklog;
import com.basic.business.pic.mapper.TGoodsPicMapper;
import com.basic.business.store.domain.TStoreMgmt;
import com.basic.business.store.mapper.TStoreMgmtMapper;
import com.basic.common.annotation.DataScope;
import com.basic.common.core.domain.entity.SysUser;
import com.basic.common.utils.DateUtils;
import com.basic.common.utils.SecurityUtils;
import com.basic.common.utils.UserUtils;
import com.basic.system.mapper.SysDeptMapper;
import org.springframework.stereotype.Service;
import com.basic.common.core.service.impl.BaseServiceImpl;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;


/**
 * 下滑预警待办事项Service业务层处理
 *
 * @author zhaoxy_jn@163.com
 */
@Service
public class TDeclineBacklogServiceImpl extends BaseServiceImpl<TDeclineBacklogMapper, TDeclineBacklog> implements ITDeclineBacklogService {

    @Resource
    private TDeclineBacklogMapper tDeclineBacklogMapper;

    @Resource
    private TStoreMgmtMapper tStoreMgmtMapper;

    @Resource
    private TLabelMappingMapper tLabelMappingMapper;

    @Resource
    private SysDeptMapper sysDeptMapper;

    @Resource
    private TGoodsPicMapper tGoodsPicMapper;


    @Override
    public int insertBacklog(TDeclineBacklog tDeclineBacklog) {

        //负责人
        TLabelMapping leader = tLabelMappingMapper.getUserByLabel(tDeclineBacklog.getGoodsLabel());

        if (leader != null) {
            tDeclineBacklog.setLeaderId(leader.getMainId());
            tDeclineBacklog.setLeaderName(leader.getMainLabel());
        } else {
            //店铺负责人
            SysUser storeLeader = tStoreMgmtMapper.getUserByStoreId(tDeclineBacklog.getStoreId());
            if (storeLeader != null) {
                tDeclineBacklog.setLeaderId(storeLeader.getUserId());
                tDeclineBacklog.setLeaderName(storeLeader.getNickName());
            }
        }

        tDeclineBacklog.setActionPoint("检查优化访客/转化率/客单价的影响因素");
        tDeclineBacklog.setCreateTime(DateUtils.getNowDate());
        tDeclineBacklog.setCreateBy(SecurityUtils.getUsername());
        int insert = tDeclineBacklogMapper.insert(tDeclineBacklog);

        if (insert > 0) {
            //异步发送钉钉消息
            CompletableFuture.runAsync(() -> sendDingTalkMessage(tDeclineBacklog));
        }

        return insert;
    }

    /**
     * 发送待办事项钉钉消息
     *
     * @param log
     */
    public void sendDingTalkMessage(TDeclineBacklog log) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        TStoreMgmt tStoreMgmt = tStoreMgmtMapper.get(log.getStoreId());

        String content = String.format("\uD83D\uDCE5 [温馨提示]下滑预警待优化通知，请相关人员查看\n" +
                "店铺名称：%s \n" +
                "商品ID：%s \n" +
                "商品标签：%s \n" +
                "商品判定类型：%s \n" +
                "行动点：%s \n" +
                "负责人：%s \n" +
                "生成时间：%s", tStoreMgmt.getStoreName(), log.getGoodsId(), log.getGoodsLabel(), "支付金额" + log.getAmountCrease() + "%  支付买家数" + log.getPayBuyerCrease() + "%  商品访客数" + log.getGoodsVisitorCrease() + "%", log.getActionPoint(), log.getLeaderName(), sdf.format(log.getCreateTime()));

        // 通过钉钉发送消息
        if (sysDeptMapper.selectDeptIdsByDeptId(110L).contains(log.getDeptId())) {
            //3C事业部通知
            DingTalkUtils.send(content, "DUH", UserUtils.getMobileByUserId(log.getLeaderId().toString()));
        } else if (sysDeptMapper.selectDeptIdsByDeptId(111L).contains(log.getDeptId())) {
            //海报事业部通知
            DingTalkUtils.send(content, "DBH", UserUtils.getMobileByUserId(log.getLeaderId().toString()));
        }
    }

    @Override
    public int getBackLogCountByGoodsId(String goodsId) {
        return tDeclineBacklogMapper.getBackLogCountByGoodsId(goodsId);
    }

    @Override
    @DataScope(deptAlias = "t")
    public List<DeclineReplayRespVo> getOptimizeList(DeclineReplayPageReqVo declineReplayPageReqVo) {
        List<DeclineReplayRespVo> list = tDeclineBacklogMapper.getOptimizeList(declineReplayPageReqVo);
        for (DeclineReplayRespVo declineReplayRespVo : list) {
            declineReplayRespVo.setImgUrl(tGoodsPicMapper.getPicUrl(String.valueOf(declineReplayRespVo.getGoodsId())));
        }
        return list;
    }

    @Override
    @DataScope(deptAlias = "t")
    public DeclineReplayCollectRespVo getDeclineManageCollectData(DeclineReplayPageReqVo declineReplayPageReqVo) {
        return tDeclineBacklogMapper.getDeclineManageCollectData(declineReplayPageReqVo);
    }
}
